#include "clientXmlCreator.h"




QDomDocument clientXmlCreator::createDocument(int id)
{
	QDomDocument doc("rinstaller");
	QDomElement root = doc.createElement("rinstaller");
	QDomAttr idAttr = doc.createAttribute("id");
	QString idString;
	idString.setNum(id);
	idAttr.setNodeValue(idString);
	root.setAttributeNode(idAttr);
	doc.appendChild(root);
	return doc;
};


bool clientXmlCreator::addShowResp(QDomDocument &doc, QMap<QString, Application *> *applicazioni)
{
	QDomElement elem = doc.createElement("showResp");

	QMap<QString, Application*>::const_iterator i = applicazioni->begin();
	while(i != applicazioni->constEnd())
	{

		QDomElement application = doc.createElement("application");
		QDomAttr name = doc.createAttribute("name");
		QDomAttr status = doc.createAttribute("status");
		name.setNodeValue(i.key());
		status.setNodeValue(QString(i.value()->getStatusString()));
		application.setAttributeNode(name);
		application.setAttributeNode(status);
		elem.appendChild(application);
		i++;
	}
	doc.documentElement().appendChild(elem);
	qDebug() << "XML:" << doc.toString() << endl;
	return true;
};


bool clientXmlCreator::addLogResp(QDomDocument &doc, QList<QString> *apps, QMap<QString, Application *> *applicazioni)
{
	QDomElement elem = doc.createElement("logResp");

	for(int i=0; i < apps->size();i++)
	{
		QDomElement log = doc.createElement("log");
		QDomAttr app = doc.createAttribute("app");
		app.setNodeValue(apps->value(i));
		QString textString(applicazioni->value(apps->value(i))->getLog());
		QDomCDATASection text = doc.createCDATASection(textString);
		log.setAttributeNode(app);
		log.appendChild(text);
		elem.appendChild(log);
		
	}
	doc.documentElement().appendChild(elem);
	qDebug() << "XML:" << doc.toString() << endl;
	return true;
};